﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>单据打印</title>

    <link href="../Scripts/bootstrap/css/bootstrap.css" rel="stylesheet" />
    <link href="../Scripts/bootstrap/css/components-rounded.css" rel="stylesheet" />
    <link href="../Scripts/bootstrap/css/font-awesome.css" rel="stylesheet" />
    <link href="../Scripts/bootstrap/css/themes/default/style.min.css" rel="stylesheet" />

    <script type="text/javascript" src="../Scripts/QueryString.js"></script>
    <script type="text/javascript" src="../Scripts/jquery-1.7.2.min.js"></script>
    <!-- 导入配置文件. -->
    <script type="text/javascript" src="../Scripts/config.js"></script>
    <script type="text/javascript" src="../Comm/Gener.js"></script>
    <script src="../CCForm/JS/wpslib/project.js"></script>
    <script src="../CCForm/JS/wpslib/wpsjsrpcsdk.js"></script>
    <script type="text/javascript">

        var printType = 0;
        var isFrameCross = GetQueryString("isFrameCross");
        $(function () {

            $("#Msg").html("<h3>正在加载请稍候....</h3>");

            var fk_bill = GetQueryString("FK_Bill");
            var nodeID = GetQueryString("FK_Node");
            var workID = GetQueryString("WorkID");
            var flowNo = GetQueryString("FK_Flow");
            var fid = GetQueryString("FID");
            printType = GetQueryString("PrintType");
            //初始化页面信息
            var handler = new HttpHandler("BP.WF.HttpHandler.WF_WorkOpt");
            handler.AddUrlData();
            var data = handler.DoMethodReturnString("PrintDoc_Init");
            if (data.indexOf('err@') == 0) {
                alert(data);
                return;
            }

            if (data.indexOf("info@") == 0) {
                data = data.replace("info@", "");
                data = JSON.parse(data);
                if (data.length == 0) {
                    //   alert("没有绑定表单，无需打印");
                    console.log("没有绑定表单，无需打印");
                    return;
                }
                var html = "";
                html += "<fieldset>";
                html += "<legend> 表单打印 </legend>";
                html += "<ul>";

                for (var i = 0; i < data.length; i++) {
                    if (data[i].FrmType == 3) //url
                        continue;
                    html += "<li><a href='javaScript:void(0)' target=_blank onclick=PrintSignleFormIt(" + flowNo + "," + nodeID + "," + workID + "," + fid + ",\"" + data[i].No + "\")><img src='../Img/FileType/doc.gif' />" + data[i].Name + ".doc</a><br/><br/></li>";
                }
                //  html += "<li>说明：点击上面的表单名称进行RTF进行打印</li>";
                html += "</ul>";
                html += "</fieldset>";

                document.getElementById("Msg").innerHTML = html;
                return;
            }


            //如果是一个url.
            if (data.indexOf('file@') == 0) {

                data = data.replace('file@', '');
                if (data.indexOf('rtf@') != -1) {
                    window.location.href = filterXSS(data).replace('rtf@', '');
                } else {
                    data = data.replace('word@', '');
                    if (plant == 'CCFlow') {
                        data += ",WSUrl=" + basePath + + "/WF/CCForm/CCFormAPI.asmx";
                    }
                    if (plant == "JFlow") {
                        alert('还没有翻译.');
                        return;
                    }

                    SetHref(data);
                }
                return;
            }

            var ens = JSON.parse(data);
            //包含多模板
            if (data.MainTable == undefined) {
                var html = "<ul>";
                for (var i = 0; i < ens.length; i++) {
                    var en = ens[i];
                    html += "<li><a href='javascript:void(0);' onclick=PrintIt(\"" + en.MyPK + "\",\"" + flowNo + "\"," + nodeID + "," + workID + "," + fid + ",\"" + en.FrmID + "\",\"" + en.TemplateFileModel + "\") >" + en.Name + "</li>";
                }

                html += "</ul>";

                $("#Msg").html(html);
                return;
            }
            //打印Wps模板文件
            PrintDoc_WpsWord(data, fk_bill)
        });

        function PrintSignleFormIt(flowNo, nodeId, WorkID, FID, ccfromId) {
            $("#rtf").html("");
            var ens = new Entities("BP.WF.Template.Frm.FrmPrintTemplates");
            ens.Retrieve("FrmID", ccfromId);
            if (ens.length == 0) {
                alert("当前节点上没有绑定单据模板");
                return false;
            }
            if (ens.length == 1) {
                PrintIt(ens[0].MyPK, flowNo, nodeId, WorkID, FID, ccfromId);
                return;
            }

            var html = "<ul>";
            for (var i = 0; i < ens.length; i++) {
                var en = ens[i];
                //  html += "<li ><a href='?FK_Bill=" + en.No + "&FK_Flow=" + flowNo + "&FK_Node=" + nodeID + "&WorkID=" + workID + "'>" + en.Name + "</li>";
                html += "<li><a href='javascript:void(0);' onclick=PrintIt(\"" + en.MyPK + "\",\"" + flowNo + "\"," + nodeId + "," + WorkID + "," + FID + ",\"" + ccfromId + "\",\"" + en.TemplateFileModel + "\") >" + en.Name + "</li>";
            }

            html += "</ul>";
            $("#rtf").html(html);

        }

        //打印.
        function PrintIt(billNo, flowNo, nodeID, workID, FID, ccfromId, printModal) {

            var handler = new HttpHandler("BP.WF.HttpHandler.WF_WorkOpt");
            handler.AddPara("FK_Bill", billNo);
            handler.AddPara("FK_Flow", flowNo);
            handler.AddPara("FK_Node", nodeID);
            handler.AddPara("WorkID", workID);
            handler.AddPara("FID", FID);
            handler.AddPara("CCFormID", ccfromId);

            var data = handler.DoMethodReturnString("PrintDoc_Done");
            if (data.indexOf('err@') == 0) {
                alert(data);
                return;
            }
            //如果是一个url.
            if (data.indexOf('url@') == 0) {

                data = data.replace('url@', '');
                if (data.indexOf('rtf@') != -1) {
                    window.location.href = filterXSS(data).replace('rtf@', '');
                    return;
                } else {
                    data = data.replace('word@', '');
                    if (plant == 'CCFlow' && printModal == 1) {
                        data += "?WSUrl=" + basePath + "/WF/CCForm/CCFormAPI.asmx";
                    }
                    if (plant == "JFlow") {
                        alert('还没有翻译.');
                        return;
                    }
                    SetHref(data);
                    return;

                }
            }
            PrintDoc_WpsWord(JSON.parse(data), billNo)

        }
        function PrintDoc_WpsWord(frmData) {
            var billTemp = frmData.Sys_FrmPrintTemplate[0];
            var wpsTempURL = document.location.protocol + "//" + document.location.host + "/DataUser/CyclostyleFile/" + billTemp.TempFilePath;
            var mainTable = frmData.MainTable[0];
            var mapDtls = frmData.Sys_MapDtl;
            var wpsData = [];
            var childDatas = [];
            if (mapDtls.length > 0) {
                $.each(frmData.Sys_MapDtl, function (i, mapDtl) {
                    childDatas.push(frmData[mapDtl.No]);
                })
            }
            for (var key in mainTable) {
                var json =
                {
                    "name": key,
                    "text": getFiledValue(frmData.Sys_MapAttr, key, mainTable[key]),
                    "type": "text"
                };
                wpsData.push(json);
            }
            var invokeParam = {
                "fileName": wpsTempURL,
                "templateDataUrl": wpsData,
                "childDatas": childDatas

            }; //根据需求任意定义
            WpsInvoke.InvokeAsHttp(
                projInfo.type,
                projInfo.name, //wps加载项的名字，在实际项目中写上对应的名字
                "OpenDoc", //要调用的在wps加载项中的函数名
                JSON.stringify(invokeParam), //调用的在wps加载项中的函数要传递的数据，是一个json对象，根据业务系统需求任意定义
                callbackFunc, //回调函数，wps加载项中InvokeFromSystemDemo这个函数的返回值作为这个函数的参数
                true) //设置wps是否显示到最前面来
        }

        var callbackFunc = function (result) {
            if (result.status == 0) {
                //下面这行代码用来注册从wps加载项返回的消息的接收函数
                WpsInvoke.RegWebNotify(projInfo.type, projInfo.name, function (messageText) {
                    var span = document.getElementById("webnotifyspan")
                    span.innerHTML = "(次数：" + ++WebNotifycount + ")：" + messageText;
                })
                alert(result.response)
            }
            else
                alert(result.message)
        }
        /* 处理值方法 */
        function getFiledValue(Sys_MapAttr, filedName, rvalue) {
            for (var i in Sys_MapAttr) {
                if (filedName == Sys_MapAttr[i].KeyOfEn) {
                    if (Sys_MapAttr[i].MyDataType == "4") {//布尔
                        if (rvalue == "1") {//是
                            rvalue = "是";
                        } else if (rvalue == "0") {//否
                            rvalue = "否";
                        }
                    }
                }
            }
            return rvalue;
        }
        //取消退回
        function Close() {
            if (isFrameCross == 0) {
                if (window.parent != null && window.parent.returnWorkWindowClose != null) {
                    window.parent.returnWorkWindowClose("取消");
                }

            } else {
                ChildrenPostMessage("取消", "returnWorkWindowClose");
            }

        }

    </script>
    <link href="../../DataUser/Style/ccbpm.css" rel="stylesheet" type="text/css" />
</head>
<body>

    <center>
        <div id="Msg">
        </div>
    </center>
    <div id="rtf"></div>

</body>
</html>
